package com.douguo.recipe;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;

import com.douguo.recipe.network.Constant;
import com.douguo.recipe.network.GetPhoto;
import com.douguo.recipe.view.NavigationView;
import com.exchange.Controller.ExchangeDataService;
import com.exchange.Controller.ReportThread;
import com.exchange.Model.AdvertiserConfig;
import com.exchange.Public.ExchangeConstants;
import com.exchange.View.DownloadDialog;

public class ToolsActivity extends DouguoActivity {
	private ListView lv;
	SimpleAdapter listItemAdapter;
	private Button mBtnBack;
	private NavigationView mNavigation;

	List<HashMap<String, Object>> ads = new ArrayList<HashMap<String, Object>>();
	List<AdvertiserConfig> ad;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.recipelist);

		ad = ExchangeDataService.mAdvertisers;
		for (AdvertiserConfig a : ad) {
			HashMap<String, Object> item = new HashMap<String, Object>();
			item.put("name", a.adName);

			item.put("author", "作者" + ": " + a.provider);
			item.put("major", "简介" + ": " + a.adDescription);
			item.put("minor", "版本" + ": " + a.version == null ? "" : a.version);
			item.put("retry", 0);
			item.put("thumnbPath", a.adIconUrl);
			item.put("thumnb", BitmapFactory.decodeResource(getResources(),
					R.drawable.cookdefault80_downloading));

			item.put("useNetwork", Boolean.TRUE);
			item.put("gettingImg", Boolean.FALSE);
			item.put("thumnbLoaded", Boolean.FALSE);
			item.put("recipePicPath", a.adIconUrl);
			ads.add(item);
		}

		lv = (ListView) findViewById(R.id.recipeslist);

		listItemAdapter = new SimpleAdapter(this, ads,
				R.layout.recipelist_listitem, new String[] { "thumnb", "name",
						"author", "major", "minor" }, new int[] {
						R.id.recipeImg, R.id.recipeName, R.id.recipeAuthor,
						R.id.recipeMajor, R.id.recipeMinor });
		listItemAdapter.setViewBinder(new ViewBinder() {

			public boolean setViewValue(View view, Object data,
					String textRepresentation) {
				if (view instanceof TextView) {
					TextView tv = (TextView) view;
					tv.setText(textRepresentation);
				} else if (view instanceof ImageView) {
					ImageView img = (ImageView) view;
					img.setImageBitmap((Bitmap) data);
					;
				}
				return true;// must be true
			}

		});
		lv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> adapterView, View view,
					int pos, long id) {
				new ReportThread(2, getApplicationContext(), ad.get(pos), 7, 2, pos).start();
				showDownloadDialog(ad.get(pos));
			}
		});

		lv.setAdapter(listItemAdapter);
		mBtnBack = (Button) findViewById(R.id.imageButton_back);
		mBtnBack.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});

		for (int i = 0; i < ad.size(); i++) {
			getPhoto(i);
		}
		
		mNavigation = new NavigationView(this, NavigationView.STATE_MORE);
		setTitle(R.string.tools);
	}

	private void getPhoto(int position) {
		Map map = (Map) listItemAdapter.getItem(position);

		if (map.containsKey("useNetwork")
				&& map.get("useNetwork") == Boolean.TRUE
				&& ((!map.containsKey("gettingImg") || map.get("gettingImg") == Boolean.FALSE))
				&& map.get("thumnbLoaded") == Boolean.FALSE) {
			GetPhoto.getPhoto(position, (String) map.get("recipePicPath"),
					mHandler);
			map.put("gettingImg", Boolean.TRUE);
		}
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == Constant.GET_PHOTO) {
				int pos = msg.arg1;
				SoftReference<Bitmap> refBitmap = (SoftReference<Bitmap>) msg.obj;
				HashMap<String, Object> hm = ads.get(pos);
				int retryTimes = (Integer) hm.get("retry");
				String thunmbPath = (String) hm.get("thumnbPath");
				if (refBitmap != null) {
					Bitmap bitmap = refBitmap.get();
					if (bitmap != null) {
						hm.put("thumnb", bitmap);
						hm.put("thumnbLoaded", Boolean.TRUE);
						listItemAdapter.notifyDataSetChanged();
					}

				}
			}
		};
	};

	private void showDownloadDialog(final AdvertiserConfig ad) {
		AlertDialog d = DownloadDialog.getCustomeDialog(this, ad, 7);
        d.show();
	}

}
